/*
 * Copyright (c) 2015-2016,2022 Apple Inc. All Rights Reserved.
 *
 * @APPLE_LICENSE_HEADER_START@
 *
 * This file contains Original Code and/or Modifications of Original Code
 * as defined in and that are subject to the Apple Public Source License
 * Version 2.0 (the 'License'). You may not use this file except in
 * compliance with the License. Please obtain a copy of the License at
 * http://www.opensource.apple.com/apsl/ and read it before using this
 * file.
 *
 * The Original Code and all software distributed under the License are
 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
 * Please see the License for the specific language governing rights and
 * limitations under the License.
 *
 * @APPLE_LICENSE_HEADER_END@
 */

#include <AssertMacros.h>

#include "SecAppleAnchorPriv.h"
#include "SecInternalReleasePriv.h"
#include "SecCFWrappers.h"
#include <Security/SecCertificatePriv.h>

static CFDictionaryRef getAnchors(void);


bool
SecIsAppleTrustAnchorData(CFDataRef cert,
			  SecAppleTrustAnchorFlags flags)
{
    CFDictionaryRef anchors = NULL;
    CFTypeRef value = NULL;
    bool res = false;

    anchors = getAnchors();
    require(anchors, fail);

    value = CFDictionaryGetValue(anchors, cert);
    require_quiet(value, fail);

    require(isBoolean(value), fail);

    res = CFBooleanGetValue(value);


 fail:
    return res;
}


bool
SecIsAppleTrustAnchor(SecCertificateRef cert,
                      SecAppleTrustAnchorFlags flags)
{
    CFDataRef data;
    bool res = false;

    data = SecCertificateCopySHA256Digest(cert);
    require(data, fail);

    res = SecIsAppleTrustAnchorData(data, flags);
    
fail:
    CFReleaseNull(data);
    return res;
}


/* Subject: C = US, O = Apple Inc., CN = Apple Code Signing Certification Authority, */
/*          OU = Apple Certification Authority */
/* Issuer: C = US, O = Apple Inc., OU = Apple Certification Authority, CN = Apple Root CA */
/* SKID: 8E:69:A6:C4:77:42:4E:04:A5:56:42:9C:51:1F:86:DA:D2:20:8F:09 */
/* Not Before: Oct 24 17:39:41 2011 GMT, Not After : Oct 24 17:39:41 2026 GMT */
static const unsigned char AppleCodeSigningCAHash[32] = {
    0x5B, 0xDA, 0xB1, 0x28, 0x8F, 0xC1, 0x68, 0x92, 0xFE, 0xF5, 0x0C, 0x65, 0x8D, 0xB5, 0x4F, 0x1E,
    0x2E, 0x19, 0xCF, 0x8F, 0x71, 0xCC, 0x55, 0xF7, 0x7D, 0xE2, 0xB9, 0x5E, 0x05, 0x1E, 0x25, 0x62
};

/* Subject: C = US, O = Apple Inc., CN = Apple Code Signing Certification Authority, */
/*          OU = Certification Authority */
/* Issuer: C = US, O = Apple Inc., OU = Apple Certification Authority, CN = Apple Root CA */
/* SKID: 37:B3:61:24:14:B8:21:C2:D0:8A:9E:16:2F:77:05:46:1A:B2:E2:F9 */
/* Not Before: Oct 18 17:41:25 2017 GMT, Not After : Oct 16 00:00:00 2030 GMT */
static const unsigned char AppleCodeSigningCAG2Hash[32] = {
    0x76, 0x84, 0x3E, 0x4C, 0x6E, 0x3A, 0xCB, 0x21, 0x61, 0x34, 0xA5, 0x7B, 0xA8, 0xAF, 0xF5, 0x12,
    0xE3, 0x94, 0x67, 0x2B, 0x39, 0x66, 0xB9, 0x96, 0xDA, 0x80, 0xF0, 0x09, 0xEE, 0x7B, 0x46, 0x45
};

bool
SecIsAppleCodeSigningIssuer(CFDataRef issuerHash)
{
    static dispatch_once_t onceToken;
    static CFDictionaryRef csAnchors = NULL;
    dispatch_once(&onceToken, ^{
        CFMutableDictionaryRef temp = CFDictionaryCreateMutableForCFTypes(NULL);
        CFDataRef value = NULL;
        value = CFDataCreateWithBytesNoCopy(NULL, AppleCodeSigningCAHash, sizeof(AppleCodeSigningCAHash), kCFAllocatorNull);
        CFDictionarySetValue(temp, value, kCFBooleanTrue);
        CFReleaseNull(value);
        value = CFDataCreateWithBytesNoCopy(NULL, AppleCodeSigningCAG2Hash, sizeof(AppleCodeSigningCAG2Hash), kCFAllocatorNull);
        CFDictionarySetValue(temp, value, kCFBooleanTrue);
        CFReleaseNull(value);

        csAnchors = temp;
    });

    bool result = false;
    CFTypeRef value = NULL;
    require(issuerHash, fail);
    require(csAnchors, fail);

    value = CFDictionaryGetValue(csAnchors, issuerHash);
    require_quiet(value, fail);
    require(isBoolean(value), fail);

    result = CFBooleanGetValue(value);
fail:
    return result;
}

bool
SecIsAppleCodeSigningAnchor(SecCertificateRef cert)
{
    CFDataRef hash = SecCertificateCopySHA256Digest(cert);
    bool result = SecIsAppleCodeSigningIssuer(hash);
    CFReleaseNull(hash);
    return result;
}

/* subject:/C=US/O=Apple Inc./OU=Apple Certification Authority/CN=Apple Root CA */
/* SKID: 2B:D0:69:47:94:76:09:FE:F4:6B:8D:2E:40:A6:F7:47:4D:7F:08:5E */
/* Not Before: Apr 25 21:40:36 2006 GMT, Not After : Feb  9 21:40:36 2035 GMT */
/* Signature Algorithm: sha1WithRSAEncryption */
static const unsigned char AppleRootCAHash[32] = {
    0xb0, 0xb1, 0x73, 0x0e, 0xcb, 0xc7, 0xff, 0x45, 0x05, 0x14, 0x2c, 0x49, 0xf1, 0x29, 0x5e, 0x6e,
    0xda, 0x6b, 0xca, 0xed, 0x7e, 0x2c, 0x68, 0xc5, 0xbe, 0x91, 0xb5, 0xa1, 0x10, 0x01, 0xf0, 0x24
};

/* subject:/CN=Apple Root CA - G2/OU=Apple Certification Authority/O=Apple Inc./C=US */
/* SKID: C4:99:13:6C:18:03:C2:7B:C0:A3:A0:0D:7F:72:80:7A:1C:77:26:8D */
/* Not Before: Apr 30 18:10:09 2014 GMT, Not After : Apr 30 18:10:09 2039 GMT */
/* Signature Algorithm: sha384WithRSAEncryption */
static const unsigned char AppleRootG2Hash[32] = {
    0xc2, 0xb9, 0xb0, 0x42, 0xdd, 0x57, 0x83, 0x0e, 0x7d, 0x11, 0x7d, 0xac, 0x55, 0xac, 0x8a, 0xe1,
    0x94, 0x07, 0xd3, 0x8e, 0x41, 0xd8, 0x8f, 0x32, 0x15, 0xbc, 0x3a, 0x89, 0x04, 0x44, 0xa0, 0x50
};

/* subject:/CN=Apple Root CA - G3/OU=Apple Certification Authority/O=Apple Inc./C=US */
/* SKID: BB:B0:DE:A1:58:33:88:9A:A4:8A:99:DE:BE:BD:EB:AF:DA:CB:24:AB */
/* Not Before: Apr 30 18:19:06 2014 GMT, Not After : Apr 30 18:19:06 2039 GMT */
/* Signature Algorithm: ecdsa-with-SHA38 */
static const unsigned char AppleRootG3Hash[32] = {
    0x63, 0x34, 0x3a, 0xbf, 0xb8, 0x9a, 0x6a, 0x03, 0xeb, 0xb5, 0x7e, 0x9b, 0x3f, 0x5f, 0xa7, 0xbe,
    0x7c, 0x4f, 0x5c, 0x75, 0x6f, 0x30, 0x17, 0xb3, 0xa8, 0xc4, 0x88, 0xc3, 0x65, 0x3e, 0x91, 0x79
};


static void
addAnchor(CFMutableDictionaryRef anchors,
          const unsigned char *trustAnchor, size_t size,
          bool production)
{
    CFDataRef value = CFDataCreateWithBytesNoCopy(NULL, trustAnchor, size, kCFAllocatorNull);
    if (CFDictionaryGetValue(anchors, value))
        abort();
    CFDictionarySetValue(anchors, value, production ? kCFBooleanTrue : kCFBooleanFalse);
    CFReleaseSafe(value);
}


static CFDictionaryRef
getAnchors(void)
{
    static dispatch_once_t onceToken;
    static CFDictionaryRef anchors = NULL;
    dispatch_once(&onceToken, ^{
        CFMutableDictionaryRef temp;

        temp = CFDictionaryCreateMutableForCFTypes(NULL);
        addAnchor(temp, AppleRootCAHash, sizeof(AppleRootCAHash), true);
        addAnchor(temp, AppleRootG2Hash, sizeof(AppleRootG2Hash), true);
        addAnchor(temp, AppleRootG3Hash, sizeof(AppleRootG3Hash), true);


        anchors = temp;
    });
    return anchors;
}

/* subject:/C=US/O=Apple Inc./OU=Apple Certification Authority/CN=Apple Root CA */
/* SKID: 2B:D0:69:47:94:76:09:FE:F4:6B:8D:2E:40:A6:F7:47:4D:7F:08:5E */
/* Not Before: Apr 25 21:40:36 2006 GMT, Not After : Feb  9 21:40:36 2035 GMT */
/* Signature Algorithm: sha1WithRSAEncryption */
static const unsigned char AppleRootCA[1215]={
    0x30,0x82,0x04,0xBB,0x30,0x82,0x03,0xA3,0xA0,0x03,0x02,0x01,0x02,0x02,0x01,0x02,
    0x30,0x0D,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x01,0x05,0x05,0x00,0x30,
    0x62,0x31,0x0B,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x55,0x53,0x31,0x13,
    0x30,0x11,0x06,0x03,0x55,0x04,0x0A,0x13,0x0A,0x41,0x70,0x70,0x6C,0x65,0x20,0x49,
    0x6E,0x63,0x2E,0x31,0x26,0x30,0x24,0x06,0x03,0x55,0x04,0x0B,0x13,0x1D,0x41,0x70,
    0x70,0x6C,0x65,0x20,0x43,0x65,0x72,0x74,0x69,0x66,0x69,0x63,0x61,0x74,0x69,0x6F,
    0x6E,0x20,0x41,0x75,0x74,0x68,0x6F,0x72,0x69,0x74,0x79,0x31,0x16,0x30,0x14,0x06,
    0x03,0x55,0x04,0x03,0x13,0x0D,0x41,0x70,0x70,0x6C,0x65,0x20,0x52,0x6F,0x6F,0x74,
    0x20,0x43,0x41,0x30,0x1E,0x17,0x0D,0x30,0x36,0x30,0x34,0x32,0x35,0x32,0x31,0x34,
    0x30,0x33,0x36,0x5A,0x17,0x0D,0x33,0x35,0x30,0x32,0x30,0x39,0x32,0x31,0x34,0x30,
    0x33,0x36,0x5A,0x30,0x62,0x31,0x0B,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,
    0x55,0x53,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x0A,0x13,0x0A,0x41,0x70,0x70,
    0x6C,0x65,0x20,0x49,0x6E,0x63,0x2E,0x31,0x26,0x30,0x24,0x06,0x03,0x55,0x04,0x0B,
    0x13,0x1D,0x41,0x70,0x70,0x6C,0x65,0x20,0x43,0x65,0x72,0x74,0x69,0x66,0x69,0x63,
    0x61,0x74,0x69,0x6F,0x6E,0x20,0x41,0x75,0x74,0x68,0x6F,0x72,0x69,0x74,0x79,0x31,
    0x16,0x30,0x14,0x06,0x03,0x55,0x04,0x03,0x13,0x0D,0x41,0x70,0x70,0x6C,0x65,0x20,
    0x52,0x6F,0x6F,0x74,0x20,0x43,0x41,0x30,0x82,0x01,0x22,0x30,0x0D,0x06,0x09,0x2A,
    0x86,0x48,0x86,0xF7,0x0D,0x01,0x01,0x01,0x05,0x00,0x03,0x82,0x01,0x0F,0x00,0x30,
    0x82,0x01,0x0A,0x02,0x82,0x01,0x01,0x00,0xE4,0x91,0xA9,0x09,0x1F,0x91,0xDB,0x1E,
    0x47,0x50,0xEB,0x05,0xED,0x5E,0x79,0x84,0x2D,0xEB,0x36,0xA2,0x57,0x4C,0x55,0xEC,
    0x8B,0x19,0x89,0xDE,0xF9,0x4B,0x6C,0xF5,0x07,0xAB,0x22,0x30,0x02,0xE8,0x18,0x3E,
    0xF8,0x50,0x09,0xD3,0x7F,0x41,0xA8,0x98,0xF9,0xD1,0xCA,0x66,0x9C,0x24,0x6B,0x11,
    0xD0,0xA3,0xBB,0xE4,0x1B,0x2A,0xC3,0x1F,0x95,0x9E,0x7A,0x0C,0xA4,0x47,0x8B,0x5B,
    0xD4,0x16,0x37,0x33,0xCB,0xC4,0x0F,0x4D,0xCE,0x14,0x69,0xD1,0xC9,0x19,0x72,0xF5,
    0x5D,0x0E,0xD5,0x7F,0x5F,0x9B,0xF2,0x25,0x03,0xBA,0x55,0x8F,0x4D,0x5D,0x0D,0xF1,
    0x64,0x35,0x23,0x15,0x4B,0x15,0x59,0x1D,0xB3,0x94,0xF7,0xF6,0x9C,0x9E,0xCF,0x50,
    0xBA,0xC1,0x58,0x50,0x67,0x8F,0x08,0xB4,0x20,0xF7,0xCB,0xAC,0x2C,0x20,0x6F,0x70,
    0xB6,0x3F,0x01,0x30,0x8C,0xB7,0x43,0xCF,0x0F,0x9D,0x3D,0xF3,0x2B,0x49,0x28,0x1A,
    0xC8,0xFE,0xCE,0xB5,0xB9,0x0E,0xD9,0x5E,0x1C,0xD6,0xCB,0x3D,0xB5,0x3A,0xAD,0xF4,
    0x0F,0x0E,0x00,0x92,0x0B,0xB1,0x21,0x16,0x2E,0x74,0xD5,0x3C,0x0D,0xDB,0x62,0x16,
    0xAB,0xA3,0x71,0x92,0x47,0x53,0x55,0xC1,0xAF,0x2F,0x41,0xB3,0xF8,0xFB,0xE3,0x70,
    0xCD,0xE6,0xA3,0x4C,0x45,0x7E,0x1F,0x4C,0x6B,0x50,0x96,0x41,0x89,0xC4,0x74,0x62,
    0x0B,0x10,0x83,0x41,0x87,0x33,0x8A,0x81,0xB1,0x30,0x58,0xEC,0x5A,0x04,0x32,0x8C,
    0x68,0xB3,0x8F,0x1D,0xDE,0x65,0x73,0xFF,0x67,0x5E,0x65,0xBC,0x49,0xD8,0x76,0x9F,
    0x33,0x14,0x65,0xA1,0x77,0x94,0xC9,0x2D,0x02,0x03,0x01,0x00,0x01,0xA3,0x82,0x01,
    0x7A,0x30,0x82,0x01,0x76,0x30,0x0E,0x06,0x03,0x55,0x1D,0x0F,0x01,0x01,0xFF,0x04,
    0x04,0x03,0x02,0x01,0x06,0x30,0x0F,0x06,0x03,0x55,0x1D,0x13,0x01,0x01,0xFF,0x04,
    0x05,0x30,0x03,0x01,0x01,0xFF,0x30,0x1D,0x06,0x03,0x55,0x1D,0x0E,0x04,0x16,0x04,
    0x14,0x2B,0xD0,0x69,0x47,0x94,0x76,0x09,0xFE,0xF4,0x6B,0x8D,0x2E,0x40,0xA6,0xF7,
    0x47,0x4D,0x7F,0x08,0x5E,0x30,0x1F,0x06,0x03,0x55,0x1D,0x23,0x04,0x18,0x30,0x16,
    0x80,0x14,0x2B,0xD0,0x69,0x47,0x94,0x76,0x09,0xFE,0xF4,0x6B,0x8D,0x2E,0x40,0xA6,
    0xF7,0x47,0x4D,0x7F,0x08,0x5E,0x30,0x82,0x01,0x11,0x06,0x03,0x55,0x1D,0x20,0x04,
    0x82,0x01,0x08,0x30,0x82,0x01,0x04,0x30,0x82,0x01,0x00,0x06,0x09,0x2A,0x86,0x48,
    0x86,0xF7,0x63,0x64,0x05,0x01,0x30,0x81,0xF2,0x30,0x2A,0x06,0x08,0x2B,0x06,0x01,
    0x05,0x05,0x07,0x02,0x01,0x16,0x1E,0x68,0x74,0x74,0x70,0x73,0x3A,0x2F,0x2F,0x77,
    0x77,0x77,0x2E,0x61,0x70,0x70,0x6C,0x65,0x2E,0x63,0x6F,0x6D,0x2F,0x61,0x70,0x70,
    0x6C,0x65,0x63,0x61,0x2F,0x30,0x81,0xC3,0x06,0x08,0x2B,0x06,0x01,0x05,0x05,0x07,
    0x02,0x02,0x30,0x81,0xB6,0x1A,0x81,0xB3,0x52,0x65,0x6C,0x69,0x61,0x6E,0x63,0x65,
    0x20,0x6F,0x6E,0x20,0x74,0x68,0x69,0x73,0x20,0x63,0x65,0x72,0x74,0x69,0x66,0x69,
    0x63,0x61,0x74,0x65,0x20,0x62,0x79,0x20,0x61,0x6E,0x79,0x20,0x70,0x61,0x72,0x74,
    0x79,0x20,0x61,0x73,0x73,0x75,0x6D,0x65,0x73,0x20,0x61,0x63,0x63,0x65,0x70,0x74,
    0x61,0x6E,0x63,0x65,0x20,0x6F,0x66,0x20,0x74,0x68,0x65,0x20,0x74,0x68,0x65,0x6E,
    0x20,0x61,0x70,0x70,0x6C,0x69,0x63,0x61,0x62,0x6C,0x65,0x20,0x73,0x74,0x61,0x6E,
    0x64,0x61,0x72,0x64,0x20,0x74,0x65,0x72,0x6D,0x73,0x20,0x61,0x6E,0x64,0x20,0x63,
    0x6F,0x6E,0x64,0x69,0x74,0x69,0x6F,0x6E,0x73,0x20,0x6F,0x66,0x20,0x75,0x73,0x65,
    0x2C,0x20,0x63,0x65,0x72,0x74,0x69,0x66,0x69,0x63,0x61,0x74,0x65,0x20,0x70,0x6F,
    0x6C,0x69,0x63,0x79,0x20,0x61,0x6E,0x64,0x20,0x63,0x65,0x72,0x74,0x69,0x66,0x69,
    0x63,0x61,0x74,0x69,0x6F,0x6E,0x20,0x70,0x72,0x61,0x63,0x74,0x69,0x63,0x65,0x20,
    0x73,0x74,0x61,0x74,0x65,0x6D,0x65,0x6E,0x74,0x73,0x2E,0x30,0x0D,0x06,0x09,0x2A,
    0x86,0x48,0x86,0xF7,0x0D,0x01,0x01,0x05,0x05,0x00,0x03,0x82,0x01,0x01,0x00,0x5C,
    0x36,0x99,0x4C,0x2D,0x78,0xB7,0xED,0x8C,0x9B,0xDC,0xF3,0x77,0x9B,0xF2,0x76,0xD2,
    0x77,0x30,0x4F,0xC1,0x1F,0x85,0x83,0x85,0x1B,0x99,0x3D,0x47,0x37,0xF2,0xA9,0x9B,
    0x40,0x8E,0x2C,0xD4,0xB1,0x90,0x12,0xD8,0xBE,0xF4,0x73,0x9B,0xEE,0xD2,0x64,0x0F,
    0xCB,0x79,0x4F,0x34,0xD8,0xA2,0x3E,0xF9,0x78,0xFF,0x6B,0xC8,0x07,0xEC,0x7D,0x39,
    0x83,0x8B,0x53,0x20,0xD3,0x38,0xC4,0xB1,0xBF,0x9A,0x4F,0x0A,0x6B,0xFF,0x2B,0xFC,
    0x59,0xA7,0x05,0x09,0x7C,0x17,0x40,0x56,0x11,0x1E,0x74,0xD3,0xB7,0x8B,0x23,0x3B,
    0x47,0xA3,0xD5,0x6F,0x24,0xE2,0xEB,0xD1,0xB7,0x70,0xDF,0x0F,0x45,0xE1,0x27,0xCA,
    0xF1,0x6D,0x78,0xED,0xE7,0xB5,0x17,0x17,0xA8,0xDC,0x7E,0x22,0x35,0xCA,0x25,0xD5,
    0xD9,0x0F,0xD6,0x6B,0xD4,0xA2,0x24,0x23,0x11,0xF7,0xA1,0xAC,0x8F,0x73,0x81,0x60,
    0xC6,0x1B,0x5B,0x09,0x2F,0x92,0xB2,0xF8,0x44,0x48,0xF0,0x60,0x38,0x9E,0x15,0xF5,
    0x3D,0x26,0x67,0x20,0x8A,0x33,0x6A,0xF7,0x0D,0x82,0xCF,0xDE,0xEB,0xA3,0x2F,0xF9,
    0x53,0x6A,0x5B,0x64,0xC0,0x63,0x33,0x77,0xF7,0x3A,0x07,0x2C,0x56,0xEB,0xDA,0x0F,
    0x21,0x0E,0xDA,0xBA,0x73,0x19,0x4F,0xB5,0xD9,0x36,0x7F,0xC1,0x87,0x55,0xD9,0xA7,
    0x99,0xB9,0x32,0x42,0xFB,0xD8,0xD5,0x71,0x9E,0x7E,0xA1,0x52,0xB7,0x1B,0xBD,0x93,
    0x42,0x24,0x12,0x2A,0xC7,0x0F,0x1D,0xB6,0x4D,0x9C,0x5E,0x63,0xC8,0x4B,0x80,0x17,
    0x50,0xAA,0x8A,0xD5,0xDA,0xE4,0xFC,0xD0,0x09,0x07,0x37,0xB0,0x75,0x75,0x21,
};

/* subject:/CN=Apple Root CA - G2/OU=Apple Certification Authority/O=Apple Inc./C=US */
/* SKID: C4:99:13:6C:18:03:C2:7B:C0:A3:A0:0D:7F:72:80:7A:1C:77:26:8D */
/* Not Before: Apr 30 18:10:09 2014 GMT, Not After : Apr 30 18:10:09 2039 GMT */
/* Signature Algorithm: sha384WithRSAEncryption */
static const unsigned char AppleRootG2[1430]={
    0x30,0x82,0x05,0x92,0x30,0x82,0x03,0x7A,0xA0,0x03,0x02,0x01,0x02,0x02,0x08,0x01,
    0xE0,0xE5,0xB5,0x83,0x67,0xA3,0xE0,0x30,0x0D,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,
    0x0D,0x01,0x01,0x0C,0x05,0x00,0x30,0x67,0x31,0x1B,0x30,0x19,0x06,0x03,0x55,0x04,
    0x03,0x0C,0x12,0x41,0x70,0x70,0x6C,0x65,0x20,0x52,0x6F,0x6F,0x74,0x20,0x43,0x41,
    0x20,0x2D,0x20,0x47,0x32,0x31,0x26,0x30,0x24,0x06,0x03,0x55,0x04,0x0B,0x0C,0x1D,
    0x41,0x70,0x70,0x6C,0x65,0x20,0x43,0x65,0x72,0x74,0x69,0x66,0x69,0x63,0x61,0x74,
    0x69,0x6F,0x6E,0x20,0x41,0x75,0x74,0x68,0x6F,0x72,0x69,0x74,0x79,0x31,0x13,0x30,
    0x11,0x06,0x03,0x55,0x04,0x0A,0x0C,0x0A,0x41,0x70,0x70,0x6C,0x65,0x20,0x49,0x6E,
    0x63,0x2E,0x31,0x0B,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x55,0x53,0x30,
    0x1E,0x17,0x0D,0x31,0x34,0x30,0x34,0x33,0x30,0x31,0x38,0x31,0x30,0x30,0x39,0x5A,
    0x17,0x0D,0x33,0x39,0x30,0x34,0x33,0x30,0x31,0x38,0x31,0x30,0x30,0x39,0x5A,0x30,
    0x67,0x31,0x1B,0x30,0x19,0x06,0x03,0x55,0x04,0x03,0x0C,0x12,0x41,0x70,0x70,0x6C,
    0x65,0x20,0x52,0x6F,0x6F,0x74,0x20,0x43,0x41,0x20,0x2D,0x20,0x47,0x32,0x31,0x26,
    0x30,0x24,0x06,0x03,0x55,0x04,0x0B,0x0C,0x1D,0x41,0x70,0x70,0x6C,0x65,0x20,0x43,
    0x65,0x72,0x74,0x69,0x66,0x69,0x63,0x61,0x74,0x69,0x6F,0x6E,0x20,0x41,0x75,0x74,
    0x68,0x6F,0x72,0x69,0x74,0x79,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x0A,0x0C,
    0x0A,0x41,0x70,0x70,0x6C,0x65,0x20,0x49,0x6E,0x63,0x2E,0x31,0x0B,0x30,0x09,0x06,
    0x03,0x55,0x04,0x06,0x13,0x02,0x55,0x53,0x30,0x82,0x02,0x22,0x30,0x0D,0x06,0x09,
    0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x01,0x01,0x05,0x00,0x03,0x82,0x02,0x0F,0x00,
    0x30,0x82,0x02,0x0A,0x02,0x82,0x02,0x01,0x00,0xD8,0x11,0x12,0x48,0x48,0xDA,0x29,
    0x8A,0x49,0xC5,0x1C,0xC7,0xEC,0x6E,0x33,0x6D,0xFE,0x4D,0xFB,0xE0,0x1C,0xDE,0xAC,
    0x5E,0xE2,0x36,0xA7,0x24,0xF9,0x7F,0x50,0x6B,0x4C,0xCE,0xB9,0x30,0x54,0x27,0xE5,
    0xB3,0xD6,0xED,0x25,0xE6,0x30,0xB6,0x05,0x37,0x5E,0x14,0x22,0x11,0xC5,0xE8,0xAA,
    0x1B,0xD2,0xFB,0xB2,0xD2,0x09,0x95,0x38,0xA4,0xEF,0x2A,0x49,0x8C,0x5D,0x3E,0x71,
    0x66,0x03,0x38,0xFB,0x16,0xF5,0x85,0x88,0xE4,0x5A,0x92,0x0C,0x04,0x32,0xF2,0xC8,
    0x40,0xFB,0x52,0x5F,0x9F,0xF6,0xC0,0xF1,0xE3,0xBA,0x45,0xA0,0x50,0xD5,0x12,0x8B,
    0xF2,0xDD,0xDE,0x91,0x86,0x23,0xF0,0xF5,0xB6,0x72,0x2E,0x01,0xDA,0x0B,0xF6,0x2E,
    0x39,0x08,0x5F,0x19,0xA1,0x63,0x41,0x0B,0x1C,0xA7,0x94,0xC1,0x86,0xC4,0x53,0x2F,
    0x76,0xF6,0x0A,0xD7,0x0C,0xD1,0x83,0x3F,0x1A,0x53,0x19,0xF3,0x57,0xD5,0x27,0x7F,
    0xFC,0x13,0xB8,0xF8,0x92,0x8D,0xFC,0xD3,0x28,0x43,0x3C,0xB5,0x68,0x00,0x25,0x5D,
    0x27,0x62,0xD3,0xDD,0x55,0xDD,0x44,0x20,0x90,0x83,0x35,0x93,0xC5,0xBF,0xB8,0x19,
    0xFB,0x6B,0xE3,0xDC,0x08,0x42,0xE6,0xAF,0x6D,0xFA,0x9E,0x40,0xCA,0x4E,0x85,0x85,
    0x78,0x49,0xB1,0xD7,0xC3,0xC1,0x30,0x39,0x32,0xAB,0x7E,0x5F,0xAA,0xD3,0x8B,0x6F,
    0x9F,0x2D,0x1A,0x21,0x68,0x70,0x67,0xB3,0xA3,0xF1,0x98,0x41,0x6D,0x91,0x7C,0xF8,
    0xD7,0xDB,0xA8,0xE7,0x5F,0x21,0x1A,0x8C,0x33,0xBF,0x31,0x74,0xB7,0xB8,0xD1,0xF4,
    0xE0,0x22,0xF4,0xBF,0x72,0x34,0xDF,0xF7,0x81,0x4D,0x71,0x7D,0x51,0xA1,0xE2,0xB3,
    0xF0,0xD3,0x28,0x16,0x73,0x6F,0xCD,0xCC,0xAD,0x37,0x7D,0x4E,0xEB,0xAD,0x40,0xE1,
    0x3F,0x81,0xFD,0xF7,0x3D,0x0A,0x3E,0xA2,0xF1,0xBD,0x31,0x96,0x29,0x59,0xDC,0xC2,
    0x19,0x80,0x8C,0x5B,0x74,0xC6,0x2C,0xD3,0x10,0x53,0x26,0x1D,0x14,0x4F,0xC4,0xD4,
    0x81,0x66,0x3C,0x87,0x67,0x33,0x27,0x14,0x08,0xE9,0xB4,0x77,0x84,0x34,0x52,0x8F,
    0x89,0xF8,0x68,0x98,0x17,0xBF,0xC3,0xBB,0xAA,0x13,0x93,0x1F,0x5D,0x54,0x2F,0xA8,
    0xC7,0x7C,0xFB,0x0D,0x14,0xBE,0x15,0x3D,0x24,0x34,0xF2,0x9A,0xDC,0x75,0x41,0x66,
    0x22,0xB4,0x01,0xD6,0x0B,0xAF,0x90,0x9E,0x0C,0xEA,0x62,0xF8,0x9B,0x59,0x3C,0x08,
    0xE2,0x96,0x34,0xE4,0x63,0xDE,0xBC,0x37,0xD4,0xEB,0x0C,0x88,0x03,0x43,0x0B,0x50,
    0xAF,0xA0,0x34,0xDD,0x50,0x4D,0x15,0xFB,0x5A,0x24,0xD8,0x0C,0xFA,0x0C,0x63,0x9E,
    0x1F,0x03,0xB1,0xE1,0xEE,0xE1,0xAA,0x43,0xF4,0x66,0x65,0x28,0x37,0x02,0x31,0xEF,
    0x01,0xC7,0x1E,0xD1,0xCC,0x9F,0x6D,0xCA,0x54,0x3A,0x40,0xDB,0xCE,0xCF,0x4F,0x46,
    0x8B,0x4A,0x65,0x9A,0x6A,0xC6,0x68,0x6C,0xD7,0xCC,0x99,0x1B,0x47,0xB0,0x72,0xC3,
    0x77,0x8F,0xC4,0xF7,0x61,0x9C,0x74,0x1F,0xCE,0xFD,0x6B,0xA1,0xC2,0x9C,0x94,0x82,
    0xAB,0x94,0xA2,0xE7,0xBD,0x1B,0xBA,0xB9,0x70,0x39,0x95,0x17,0xC5,0x29,0xF3,0x39,
    0x58,0x34,0xF5,0xC4,0xA4,0xC6,0x7B,0x60,0xB9,0x66,0x43,0x50,0x3F,0x6E,0x61,0xFC,
    0x0E,0xF9,0x86,0xAA,0x60,0x0C,0x43,0x4B,0x95,0x02,0x03,0x01,0x00,0x01,0xA3,0x42,
    0x30,0x40,0x30,0x1D,0x06,0x03,0x55,0x1D,0x0E,0x04,0x16,0x04,0x14,0xC4,0x99,0x13,
    0x6C,0x18,0x03,0xC2,0x7B,0xC0,0xA3,0xA0,0x0D,0x7F,0x72,0x80,0x7A,0x1C,0x77,0x26,
    0x8D,0x30,0x0F,0x06,0x03,0x55,0x1D,0x13,0x01,0x01,0xFF,0x04,0x05,0x30,0x03,0x01,
    0x01,0xFF,0x30,0x0E,0x06,0x03,0x55,0x1D,0x0F,0x01,0x01,0xFF,0x04,0x04,0x03,0x02,
    0x01,0x06,0x30,0x0D,0x06,0x09,0x2A,0x86,0x48,0x86,0xF7,0x0D,0x01,0x01,0x0C,0x05,
    0x00,0x03,0x82,0x02,0x01,0x00,0x51,0xA6,0xF3,0xE2,0xF4,0xB8,0x3D,0x93,0xBF,0x2D,
    0xCE,0x0F,0xBB,0x5B,0xE1,0x55,0x14,0x4E,0x4E,0xD1,0xE5,0xCE,0x79,0x5D,0x81,0x7F,
    0xFE,0xB6,0xF0,0x87,0x33,0xF8,0xEF,0x94,0xE5,0x7E,0xDC,0x6A,0x79,0xA7,0x1C,0xBE,
    0xF0,0x94,0xB7,0xA6,0xD1,0x30,0x9C,0xC8,0x0D,0x0A,0x75,0x9E,0x7D,0x92,0x95,0x7E,
    0x18,0x9D,0x7E,0xC2,0x71,0x69,0x7C,0x14,0xEA,0xCF,0x83,0x0E,0xE4,0x14,0x42,0x9E,
    0x74,0x0E,0x10,0xCD,0xAB,0x1A,0xBA,0x11,0x61,0x81,0x78,0xD8,0xF1,0xB5,0x45,0x40,
    0x78,0xAB,0xA8,0xC0,0xCE,0xFB,0x7D,0x63,0x37,0x68,0xF6,0xE7,0xFB,0xAF,0xC6,0xC3,
    0x4B,0xEC,0x1F,0x36,0x26,0x13,0x54,0x86,0x94,0x72,0xB2,0xEA,0x02,0xED,0x8B,0x6D,
    0xE4,0x0C,0xA6,0x90,0xC0,0x57,0x75,0xCF,0x8C,0x42,0x7D,0x5C,0xE6,0x31,0x7D,0xF3,
    0xC9,0xB2,0x92,0x69,0x46,0x0E,0x88,0xF8,0xE3,0x2D,0x42,0xB2,0x38,0xA8,0xA6,0x19,
    0x8D,0xF1,0x9F,0xCD,0xEE,0x6A,0x65,0xBC,0x1A,0xB0,0x25,0xBD,0xA7,0x29,0xFD,0xF4,
    0x3E,0xA2,0x75,0x49,0xBF,0x9E,0xDB,0xC9,0xF7,0xA7,0x1E,0x63,0x99,0xE1,0x5C,0x46,
    0xFF,0x92,0x05,0x8C,0xFA,0x1E,0x20,0xF9,0x86,0x94,0x56,0x25,0xE5,0xB4,0x57,0x38,
    0x9D,0xEB,0x88,0x64,0x14,0x21,0x49,0x21,0x39,0xBF,0x62,0x66,0xA9,0xB1,0xA2,0xCA,
    0x6F,0x3F,0x21,0x60,0xC5,0x89,0xD4,0x45,0x36,0xC8,0x98,0x7C,0xBD,0xF6,0xFE,0x99,
    0x49,0x80,0x3B,0x2C,0xD2,0xA6,0xA7,0x88,0x03,0x04,0x31,0x19,0xB7,0xB6,0x3A,0x61,
    0x45,0xFA,0xC9,0xF2,0x23,0xC8,0x63,0x73,0xBF,0x56,0x89,0x31,0xB0,0xD9,0x7C,0x62,
    0xA7,0x7B,0x15,0xA8,0x88,0x8A,0xAB,0x38,0x40,0xC2,0xCC,0x12,0xFF,0x15,0xE3,0xF0,
    0x37,0xDF,0x37,0x72,0xCB,0xCC,0x98,0xE6,0xBF,0xA2,0xBC,0xFA,0x26,0x8A,0x71,0x56,
    0xD7,0xE7,0x24,0x1B,0x48,0x44,0x3E,0x9E,0xFC,0x9F,0xC9,0xCC,0x1A,0xEC,0x43,0x3C,
    0x01,0xBC,0x34,0x78,0xC8,0x69,0xF5,0xC6,0xE6,0x56,0xEC,0x06,0x09,0x36,0x90,0xEB,
    0x14,0x4A,0x1B,0x5E,0xC9,0x88,0x23,0xDA,0x03,0x30,0x91,0x0B,0xB8,0x36,0x3E,0xF9,
    0xE7,0xB5,0x28,0x6F,0xBE,0x3F,0xEC,0x3C,0x8F,0x65,0x1D,0xE5,0xC0,0x1E,0x87,0xA4,
    0xAA,0xBA,0x98,0xFD,0x92,0xE3,0x6C,0x26,0x77,0xDD,0x06,0xB4,0x64,0x06,0x87,0xF4,
    0x4E,0xD6,0xBA,0x4A,0xAA,0x16,0xA8,0xF4,0x05,0x67,0x66,0x96,0xBA,0xE2,0x55,0x79,
    0xC3,0x2C,0x5D,0x49,0x8F,0x80,0x49,0x2B,0x8A,0x12,0xC7,0x76,0x80,0x51,0xDF,0xBA,
    0xBD,0x65,0x5D,0x3E,0x37,0x47,0x63,0x31,0xE9,0xE5,0xF4,0xC5,0x3F,0x4B,0xAD,0x04,
    0x8A,0x7A,0x71,0x2C,0xAF,0x09,0x43,0x37,0x0F,0xA8,0xE3,0x32,0x4F,0xF4,0x45,0xB6,
    0x6D,0x97,0x36,0xEC,0x84,0xF5,0x0A,0x01,0xEA,0x17,0xBB,0x85,0x8D,0x42,0x93,0x70,
    0xC3,0x50,0xE5,0x14,0x8B,0xBF,0x3F,0xC3,0x41,0x0F,0xDD,0x22,0x04,0x23,0x08,0x8A,
    0xBA,0x6D,0x71,0x44,0xAB,0x73,0x09,0x3A,0xC9,0xF9,0x52,0x80,0x09,0xDF,0xBA,0xE9,
    0xE6,0x16,0xCA,0x2E,0x2E,0x4C,0xB2,0xD3,0xDC,0xE5,0x04,0x54,0xB2,0xD4,0x34,0x80,
    0x32,0xB5,0xBC,0x0F,0x17,0xE1,
};

/* subject:/CN=Apple Root CA - G3/OU=Apple Certification Authority/O=Apple Inc./C=US */
/* SKID: BB:B0:DE:A1:58:33:88:9A:A4:8A:99:DE:BE:BD:EB:AF:DA:CB:24:AB */
/* Not Before: Apr 30 18:19:06 2014 GMT, Not After : Apr 30 18:19:06 2039 GMT */
/* Signature Algorithm: ecdsa-with-SHA38 */
static const unsigned char AppleRootG3[583]={
    0x30,0x82,0x02,0x43,0x30,0x82,0x01,0xC9,0xA0,0x03,0x02,0x01,0x02,0x02,0x08,0x2D,
    0xC5,0xFC,0x88,0xD2,0xC5,0x4B,0x95,0x30,0x0A,0x06,0x08,0x2A,0x86,0x48,0xCE,0x3D,
    0x04,0x03,0x03,0x30,0x67,0x31,0x1B,0x30,0x19,0x06,0x03,0x55,0x04,0x03,0x0C,0x12,
    0x41,0x70,0x70,0x6C,0x65,0x20,0x52,0x6F,0x6F,0x74,0x20,0x43,0x41,0x20,0x2D,0x20,
    0x47,0x33,0x31,0x26,0x30,0x24,0x06,0x03,0x55,0x04,0x0B,0x0C,0x1D,0x41,0x70,0x70,
    0x6C,0x65,0x20,0x43,0x65,0x72,0x74,0x69,0x66,0x69,0x63,0x61,0x74,0x69,0x6F,0x6E,
    0x20,0x41,0x75,0x74,0x68,0x6F,0x72,0x69,0x74,0x79,0x31,0x13,0x30,0x11,0x06,0x03,
    0x55,0x04,0x0A,0x0C,0x0A,0x41,0x70,0x70,0x6C,0x65,0x20,0x49,0x6E,0x63,0x2E,0x31,
    0x0B,0x30,0x09,0x06,0x03,0x55,0x04,0x06,0x13,0x02,0x55,0x53,0x30,0x1E,0x17,0x0D,
    0x31,0x34,0x30,0x34,0x33,0x30,0x31,0x38,0x31,0x39,0x30,0x36,0x5A,0x17,0x0D,0x33,
    0x39,0x30,0x34,0x33,0x30,0x31,0x38,0x31,0x39,0x30,0x36,0x5A,0x30,0x67,0x31,0x1B,
    0x30,0x19,0x06,0x03,0x55,0x04,0x03,0x0C,0x12,0x41,0x70,0x70,0x6C,0x65,0x20,0x52,
    0x6F,0x6F,0x74,0x20,0x43,0x41,0x20,0x2D,0x20,0x47,0x33,0x31,0x26,0x30,0x24,0x06,
    0x03,0x55,0x04,0x0B,0x0C,0x1D,0x41,0x70,0x70,0x6C,0x65,0x20,0x43,0x65,0x72,0x74,
    0x69,0x66,0x69,0x63,0x61,0x74,0x69,0x6F,0x6E,0x20,0x41,0x75,0x74,0x68,0x6F,0x72,
    0x69,0x74,0x79,0x31,0x13,0x30,0x11,0x06,0x03,0x55,0x04,0x0A,0x0C,0x0A,0x41,0x70,
    0x70,0x6C,0x65,0x20,0x49,0x6E,0x63,0x2E,0x31,0x0B,0x30,0x09,0x06,0x03,0x55,0x04,
    0x06,0x13,0x02,0x55,0x53,0x30,0x76,0x30,0x10,0x06,0x07,0x2A,0x86,0x48,0xCE,0x3D,
    0x02,0x01,0x06,0x05,0x2B,0x81,0x04,0x00,0x22,0x03,0x62,0x00,0x04,0x98,0xE9,0x2F,
    0x3D,0x40,0x72,0xA4,0xED,0x93,0x22,0x72,0x81,0x13,0x1C,0xDD,0x10,0x95,0xF1,0xC5,
    0xA3,0x4E,0x71,0xDC,0x14,0x16,0xD9,0x0E,0xE5,0xA6,0x05,0x2A,0x77,0x64,0x7B,0x5F,
    0x4E,0x38,0xD3,0xBB,0x1C,0x44,0xB5,0x7F,0xF5,0x1F,0xB6,0x32,0x62,0x5D,0xC9,0xE9,
    0x84,0x5B,0x4F,0x30,0x4F,0x11,0x5A,0x00,0xFD,0x58,0x58,0x0C,0xA5,0xF5,0x0F,0x2C,
    0x4D,0x07,0x47,0x13,0x75,0xDA,0x97,0x97,0x97,0x6F,0x31,0x5C,0xED,0x2B,0x9D,0x7B,
    0x20,0x3B,0xD8,0xB9,0x54,0xD9,0x5E,0x99,0xA4,0x3A,0x51,0x0A,0x31,0xA3,0x42,0x30,
    0x40,0x30,0x1D,0x06,0x03,0x55,0x1D,0x0E,0x04,0x16,0x04,0x14,0xBB,0xB0,0xDE,0xA1,
    0x58,0x33,0x88,0x9A,0xA4,0x8A,0x99,0xDE,0xBE,0xBD,0xEB,0xAF,0xDA,0xCB,0x24,0xAB,
    0x30,0x0F,0x06,0x03,0x55,0x1D,0x13,0x01,0x01,0xFF,0x04,0x05,0x30,0x03,0x01,0x01,
    0xFF,0x30,0x0E,0x06,0x03,0x55,0x1D,0x0F,0x01,0x01,0xFF,0x04,0x04,0x03,0x02,0x01,
    0x06,0x30,0x0A,0x06,0x08,0x2A,0x86,0x48,0xCE,0x3D,0x04,0x03,0x03,0x03,0x68,0x00,
    0x30,0x65,0x02,0x31,0x00,0x83,0xE9,0xC1,0xC4,0x16,0x5E,0x1A,0x5D,0x34,0x18,0xD9,
    0xED,0xEF,0xF4,0x6C,0x0E,0x00,0x46,0x4B,0xB8,0xDF,0xB2,0x46,0x11,0xC5,0x0F,0xFD,
    0xE6,0x7A,0x8C,0xA1,0xA6,0x6B,0xCE,0xC2,0x03,0xD4,0x9C,0xF5,0x93,0xC6,0x74,0xB8,
    0x6A,0xDF,0xAA,0x23,0x15,0x02,0x30,0x6D,0x66,0x8A,0x10,0xCA,0xD4,0x0D,0xD4,0x4F,
    0xCD,0x8D,0x43,0x3E,0xB4,0x8A,0x63,0xA5,0x33,0x6E,0xE3,0x6D,0xDA,0x17,0xB7,0x64,
    0x1F,0xC8,0x53,0x26,0xF9,0x88,0x62,0x74,0x39,0x0B,0x17,0x5B,0xCB,0x51,0xA8,0x0C,
    0xE8,0x18,0x03,0xE7,0xA2,0xB2,0x28,
};


static void
addCertificate(CFMutableArrayRef anchors,
               const unsigned char *anchor, size_t size) {
    SecCertificateRef cert = SecCertificateCreateWithBytes(NULL, anchor, size);
    if (CFArrayContainsValue(anchors, CFRangeMake(0, CFArrayGetCount(anchors)), cert)) {
        abort();
    }
    CFArrayAppendValue(anchors, cert);
    CFReleaseNull(cert);
}

CFArrayRef SecGetAppleTrustAnchors(bool allowNonProduction)
{
    static CFArrayRef anchors = NULL;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        CFMutableArrayRef temp = NULL;
        temp = CFArrayCreateMutableForCFTypesWithCapacity(NULL, 3);

        addCertificate(temp, AppleRootCA, sizeof(AppleRootCA));
        addCertificate(temp, AppleRootG2, sizeof(AppleRootG2));
        addCertificate(temp, AppleRootG3, sizeof(AppleRootG3));

        anchors = temp;
    });
    return anchors;
}
